package com.example.housekeeping.service.impl;

import com.example.housekeeping.ex.ServiceException;
import com.example.housekeeping.pojo.entity.Orders;
import com.example.housekeeping.pojo.dto.OrdersAddNewDTO;
import com.example.housekeeping.pojo.dto.OrdersUpdateDTO;
import com.example.housekeeping.pojo.vo.OrdersListItemVO;
import com.example.housekeeping.pojo.vo.OrdersStandardVO;
import com.example.housekeeping.mapper.OrdersMapper;
import com.example.housekeeping.service.OrdersService;
import com.example.housekeeping.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单(Orders)表服务实现类
 *
 * @author bob
 * @since 2023-03-20 19:36:07
 */
@Slf4j
@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 新增数据
     *
     * @param ordersAddNewDTO 实例对象
     * @return 实例对象
     */
    @Override
    public void addNew(OrdersAddNewDTO ordersAddNewDTO) {
        log.debug("开始处理【添加订单】的业务，参数：{}", ordersAddNewDTO);
        OrdersStandardVO queryResult = ordersMapper.getStandardByWorkerId(ordersAddNewDTO.getWorkerId());
        if (queryResult != null && queryResult.getState() != 5){
            String message = "该订单已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        //创建orders对象
        Orders orders = new Orders();
        //复制属性
        BeanUtils.copyProperties(ordersAddNewDTO,orders);
        orders.setCreated(new Date());
        orders.setAppointmentTime(new Date());

        //执行插入insert()方法
        int rows = ordersMapper.insert(orders);
        if (rows != 1){
            String message = "新增订单失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }


    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除订单】业务，参数：{}",id);
        OrdersStandardVO queryResult = ordersMapper.getStandardById(id);
        if (queryResult == null){
            String message = "当前编号订单不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        int rows = ordersMapper.deleteById(id);
        if (rows != 1){
            String message = "删除订单失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }

    }

    /**
     * 修改数据
     *
     * @param ordersUpdateDTO 实例对象
     * @return 实例对象
     */
    @Override
    public void updateById(Long id, OrdersUpdateDTO ordersUpdateDTO) {
        log.debug("开始处理【修改订单详情】的业务，ID：{}，新数据：{}", id, ordersUpdateDTO);
        OrdersStandardVO queryResult = ordersMapper.getStandardById(id);
        if (queryResult == null){
            // 是：抛出异常
            String message = "修改订单详情失败，尝试修改的订单数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //是否需要判断

        //创建orders对象
        Orders orders = new Orders();
        //复制属性
        BeanUtils.copyProperties(ordersUpdateDTO,orders);
        //设置id
        orders.setId(id);
        //调用update方法
        ordersMapper.update(orders);

    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OrdersStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询订单详情】的业务，参数：{}", id);
        OrdersStandardVO queryResult = ordersMapper.getStandardById(id);
        if (queryResult == null){
            // 是：抛出异常
            String message = "查询订单详情失败，订单数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    /**
     * 查询列表数据
     *
     * @return 实例对象
     */
    @Override
    public List<OrdersListItemVO> list() {
        log.debug("开始处理【查询订单列表】的业务，无参数");
         List<OrdersListItemVO> list = ordersMapper.list();
        return list;
    }

    @Override
    public Orders getNotFinishByWorkerId(Long workerId) {
        log.debug("开始处理[根据服务员id查询未完成订单]业务");
        return ordersMapper.getNotFinishByWorkerId(workerId);
    }

}
